package com.qfedu.util;

public class TransactionManager {

        private ConnectionUtils connectionUtils;

        public void setConnectionUtils(ConnectionUtils connectionUtils) {
            this.connectionUtils = connectionUtils;
        }

        /**
         * 开启事务
         */
        public void beginTransaction() {
            try {
                System.out.println("kaishi");
                //获取绑定了当前线程的连接，并且关闭自动提交事务
                connectionUtils.getThreadConnection().setAutoCommit(false);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    public void commit() {
        try {
            System.out.println("tijiao");
            connectionUtils.getThreadConnection().commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 回滚事务
     */
    public void rollback() {
        try {
            System.out.println("gun");
            connectionUtils.getThreadConnection().rollback();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 释放连接
     */
    public void release() {
        try {
            System.out.println("shifang");
            //归还到服务器线程池中 此时当前线程和数据库连接对象并没有解绑
            connectionUtils.getThreadConnection().close();
            //解绑
            connectionUtils.removeConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
